

//
//  ResetPSWVC.m
//  ImGuider
//
//  Created by llt on 2017/4/20.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "ResetPSWVC.h"
#import "SelectCountryVC.h"
#import <IQKeyboardManager.h>

@interface ResetPSWVC ()

@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *pswLabel;
@property (weak, nonatomic) IBOutlet UILabel *verifyLabel;

@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *pswTF;
@property (weak, nonatomic) IBOutlet UITextField *verifyTF;

@property (weak, nonatomic) IBOutlet UIButton *countryBtn;
@property (weak, nonatomic) IBOutlet UIButton *verifyCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *resetBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contryBtnWidth;

@property (nonatomic, strong) NSTimer *countDownTimer;

@end

@implementation ResetPSWVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setup {
    
    self.isHiddenPlayStatusButton = YES;
    self.barBGAlpha = 0;
    self.customBarStyle = UIStatusBarStyleLightContent;
    
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    self.view.backgroundColor = kThemeColor;
    self.title = MYLocalizedString(@"LOGIN_RESETPSW", nil);
    
    self.phoneLabel.text = MYLocalizedString(@"REGISTER_MOBILE", nil);
    self.pswLabel.text = MYLocalizedString(@"REGISTER_PSW", nil);
    self.verifyLabel.text = MYLocalizedString(@"REGISTER_CODE", nil);
    
    self.phoneNumTF.placeholder = MYLocalizedString(@"REGISTER_MOBILE_PLACEHOLDER", nil);
    self.pswTF.placeholder = MYLocalizedString(@"REGISTER_PSW_PLACEHOLDER", nil);
    self.verifyTF.placeholder = MYLocalizedString(@"REGISTER_CODE_PLACEHOLDER", nil);
    
    [self.resetBtn setTitle:MYLocalizedString(@"RESETPSW_SUBMIT", nil) forState:UIControlStateNormal];
    [self.countryBtn setTitle:MYLocalizedString(@"REGISTER_PLACE", nil) forState:UIControlStateNormal];
    [self.verifyCodeBtn setTitle:MYLocalizedString(@"REGISTER_GET_CODE", nil) forState:UIControlStateNormal];

    self.countryBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    [self.countryBtn.titleLabel setMinimumScaleFactor:0.5];

    self.phoneNumTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.phoneNumTF.placeholder attributes:@{NSForegroundColorAttributeName : kBGColor}];
    self.pswTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.pswTF.placeholder attributes:@{NSForegroundColorAttributeName : kBGColor}];
    self.verifyTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.verifyTF.placeholder attributes:@{NSForegroundColorAttributeName : kBGColor}];
    
    self.resetBtn.layer.borderWidth = 1.0f;
    self.resetBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    self.resetBtn.layer.cornerRadius = 5.0f;
    
    if (self.country) {
        [self selectedCountry:self.country];
    }
    
    if (self.loginName) {
        self.phoneNumTF.text = self.loginName;
    }
    
}

- (IBAction)showPSW:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    self.pswTF.secureTextEntry = !sender.selected;
    [MobClick event:@"11_0_1"];
}

- (IBAction)getCode:(UIButton *)sender {
    
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    
    NSInteger tag = sender.tag;
    if (tag > 0) return;
    
    NSString* mobile = [self.phoneNumTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (!self.country) {
        [IGUtil showToast:MYLocalizedString(@"REGISTER_NOCOUNTRY", nil)];
        return;
    }
    
    if ([IGUtil isStringLenght0:mobile]) {
        [IGUtil showToast:MYLocalizedString(@"REGISTER_NOMOBILE", nil)];
        return;
    }
    
    sender.tag = 60; // 作为倒计时的标记
    if (self.countDownTimer != nil) {
        
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
    }
    self.countDownTimer = [NSTimer timerWithTimeInterval:1.0
                                              target:self
                                            selector:@selector(updateSendText)
                                            userInfo:nil
                                             repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.countDownTimer forMode:NSDefaultRunLoopMode];
    [self updateSendText];
    

    [ActivityIndicatorView showHUDAddedTo:self.view tintColor:[UIColor whiteColor]];
    
//    0注册，认证，1找回密码,2改密码
    [NetAPIManager getCodeWithCountryCode:self.country.code cellPhone:mobile usage:1 Block:^(NSString *result, NSError *error) {

        [ActivityIndicatorView hideHUDAddedTo:self.view];
        if (error) {
            sender.tag = 0;
        }
        [IGUtil showToast:result];
    }];
}

- (void)updateSendText {

    NSInteger tag = self.verifyCodeBtn.tag;
    NSString* title = tag > 0 ?  [NSString stringWithFormat:@"%@(%ld)",MYLocalizedString(@"REGISTER_REGET_CODE", nil),(long)tag] : MYLocalizedString(@"REGISTER_REGET_CODE", nil);
    UIColor* color = tag > 0 ? kContentColor : [UIColor whiteColor];
    [self.verifyCodeBtn setTitle:title forState:UIControlStateNormal];
    [self.verifyCodeBtn setTitleColor:color forState:UIControlStateNormal];


    if (tag == 0) {
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
    }
     
    [self.verifyCodeBtn setTag:tag -1];
}

- (IBAction)commitBtnClick:(id)sender {
    
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    
    NSString* mobile = [self.phoneNumTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (!self.country) {
        [IGUtil showToast:MYLocalizedString(@"REGISTER_NOCOUNTRY", nil)];
        return;
    }
    
    if ([IGUtil isStringLenght0:mobile]) {
        [IGUtil showToast:MYLocalizedString(@"REGISTER_NOMOBILE", nil)];
        return;
    }
    
    if ([IGUtil isStringLenght0:self.verifyTF.text]) {
        [IGUtil showToast:MYLocalizedString(@"REGISTER_NOCODE", nil)];
        return;
    }
    if ([IGUtil isStringLenght0:self.pswTF.text]) {
        [IGUtil showToast:MYLocalizedString(@"REGISTER_NOPSW", nil)];
        return;
    }
    [ActivityIndicatorView showHUDAddedTo:self.view tintColor:[UIColor whiteColor]];
    [NetAPIManager resetPswWithCountryCode:self.country.code cellPhone:mobile psw:self.pswTF.text encode:self.verifyTF.text block:^(NSString *result, NSError *error) {
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        if (!error) {
            
            [_delegate resetSuccess:self];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (IBAction)selectCountry:(UIButton *)sender {
    
    SelectCountryVC *countryVC = [[SelectCountryVC alloc] init];
    countryVC.delegate = self;
    [countryVC showWithVC:self];
}

#pragma mark -SelectCountryVCDelegate
- (void)selectedCountry:(Country *)country {
    
    [self.countryBtn setTitle:country.code forState:UIControlStateNormal];
    [self.countryBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.country = country;
    
    self.contryBtnWidth.constant = 60;
    [self.countryBtn setImage:[UIImage imageNamed:@"下拉"] forState:UIControlStateNormal];
    
    CGFloat imageWith = CGRectGetWidth(self.countryBtn.imageView.frame);
    
    CGFloat labelWidth = [IGUtil getSizeContent:country.code width:MAXFLOAT font:FontBold(14)].width;
    
    self.countryBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
    self.countryBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWith, 0, imageWith);
}

@end
